<?php

class BrowseController extends Controller
{
	public function actionIndex()
	{
		$userFb = Yii::app()->params['userFb'];
        $id = $userFb->getId();
	
		$this->render('index',array("id" => $id));
	}

	public function actionViewLatest(){
		//browse semua job diurutkan berdasarkan waktu (DESC), dan status = 1 (active)
		$job = new Job();

		// uncomment the following code to enable ajax-based validation
		/*
		if(isset($_POST['ajax']) && $_POST['ajax']==='job-searchResults-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		*/

		if(isset($_POST['Job']))
		{
			$job->attributes=$_POST['Job'];
                        //$job->rewardMax = $_POST['Job']['rewardMax'];
//			if($job->validate('searchJob'))
//			{
				// form inputs are valid, do something here
                        //condition dapet dari form
                        //bikin string one line buat query
                        $query = $this->searchQueryGenerator($job);
                        $param = $this->searchParamsGenerator($job);
                             $criteria = array(
                                'condition' => $query,
                                'params' => $param,
                                'order' => 'start_date DESC'
                            );
                         //die(print_r($job->attributes, true));
			$this->render('listJob', array("type" => "criteria", "model" => $job, "cond" => $criteria));
//			}
		}
                else{
                    $criteria = array(
                            'condition' => 'status = :status',
                            'params' => array(':status' => 1),
                            'order' => 'start_date DESC'
                    );
                    $this->render('listJob', array("type" => "latest", "model" => $job, "cond" => $criteria));
                }
                
		
	}
	
	//menghasilkan string dari array kondisi buat dimasukin ke 'condition' di fungsi actionViewByCriteria()
	public function searchQueryGenerator($job){
             $str='';

             if ($job->name != null){
                 if ($job->reward != null || $job->start_date != null){
                    $str .= 'name REGEXP :name AND ';
                 }
                 else{
                    $str .= 'name REGEXP :name';
                 }
             }
             else{

             }

             if ($job->reward != null && $job->rewardMax != null){
                 if ($job->start_date != null){
                    $str .= 'reward BETWEEN :reward AND :rewardMax AND ';
                 }
                 else{
                    $str .= 'reward BETWEEN :reward AND :rewardMax';
                 }
             }

              if ($job->start_date != null && $job->start_dateMax != null){
                    $str .= 'start_date BETWEEN :start_date AND :start_dateMax';
              }

              $str .= ' AND status = :status ';

              return $str;
	}
        
        public function searchParamsGenerator($job){
            
            if ($job->name != null){
                $arr = array(':name' => $job->name);
            }
            if ($job->reward != null){
                $arr[':reward'] = $job->reward;
            }
            if ($job->rewardMax != null){
                $arr[':rewardMax'] = $job->rewardMax;
            }
            if ($job->start_date != null){
                $arr['start_date'] = $job->start_date;
            }
            if ($job->start_dateMax != null){
                $arr[':start_dateMax'] = $job->start_dateMax;
            }
            $arr[':status'] = 1;

            return $arr;
            
        }
	
	/*
		strcond = array(columns=array(name,sign), operator)
	*/
	public function actionViewByCriteria(/*$strCond, $values*/){
		//browse semua job berdasarkan kriteria
		//buat kriteria di sini
		$job = new Job();
		
		$criteria = array(
			'condition' => 'reward > :reward AND status = :status',
			'params' => array(':reward'=> 1000000, ':status' => 0),
			'order' => 'start_date DESC'
		);
		$this->render('listJob', array("type" => "criteria", "model" => $job, "cond" => $criteria));
	}
	
	public function actionSearchResults()
	{
		$model=new Job();

		// uncomment the following code to enable ajax-based validation
		/*
		if(isset($_POST['ajax']) && $_POST['ajax']==='job-searchResults-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		*/

		if(isset($_POST['Job']))
		{
			$model->attributes=$_POST['Job'];
			if($model->validate())
			{
				// form inputs are valid, do something here
				
				return;
			}
		}
		$this->render('searchResults',array('formModel'=>$model));
	}
	
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}